package com.yjc.drygoods.order.service.impl;

import com.rabbitmq.client.Channel;
import com.yjc.drygoods.order.entity.OrderEntity;
import com.yjc.drygoods.order.service.OrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author yjc
 * @create 2022-09-28 8:31
 */

@RabbitListener(queues = {"order.release.order.queue"})
@Service
public class OrderCloseListener {

    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void closeOrder(OrderEntity order, Message message, Channel channel) throws IOException {
        try {
            orderService.closeOrder(order);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

    }
}
